package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    //图片
    @Autowired
    private GraphInfoService graphInfoService;
    //配套
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    //杂费
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    //标签
    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private RoomInfoService roomInfoService;

    //分页查询
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    //标签
    @Autowired
    private LabelInfoMapper labelInfoMapper;

    //配套信息
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    //杂费
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private DistrictInfoService districtInfoService;

    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Override
    public void saveAparement(ApartmentSubmitVo apartmentSubmitVo) {
        boolean b=apartmentSubmitVo.getId()!=null;
        //保存

        //解决联调时，公寓信息不显示地区问题
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        saveOrUpdate(apartmentSubmitVo);
        //如果存在，就先删除再保存
        if(b){
            //删除图片
            LambdaQueryWrapper<GraphInfo> GraphLambdaQueryWrapper = new LambdaQueryWrapper<>();
            GraphLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            GraphLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(GraphLambdaQueryWrapper);

            //删除配套
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper= new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

            //删除标签
            LambdaQueryWrapper<ApartmentLabel> ApartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(ApartmentLabelQueryWrapper);

            //删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> AparementFeevlaQueryWrapper = new LambdaQueryWrapper<>();
            AparementFeevlaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(AparementFeevlaQueryWrapper);
        }
        //插入图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();

        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {

                GraphInfo graphInfo = new GraphInfo();

                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //插入公寓配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility facility = ApartmentFacility.builder()
                        .facilityId(facilityInfoId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                facilityList.add(facility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }
        //插入杂费
        //公寓信息集合
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            //公寓信息内部杂费值集合
            ArrayList<ApartmentFeeValue> feevaluesList = new ArrayList<>();
            //在对应的公寓信息下添加对应的杂费值信息
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = ApartmentFeeValue.builder()
                        .feeValueId(feeValueId)
                        .apartmentId(apartmentSubmitVo.getId())
                        .build();
                feevaluesList.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(feevaluesList);
        }
        //插入标签信息
        //公寓的标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //如果公寓标签信息是空的，我们就进行新增
        if(!CollectionUtils.isEmpty(labelIds)){
            //不存在，创建对应的标签信息集合
            ArrayList<ApartmentLabel> labelList = new ArrayList<>();
            //在公寓标签信息中循环添加具体的标签信息
            for (Long labelId : labelIds) {
                //对应添加标签信息id...
                ApartmentLabel label = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId)
                        .build();
                //将具体信息加到标签信息集合
                labelList.add(label);
            }
            apartmentLabelService.saveBatch(labelList);
        }
    }

    //根据分页进行查询公寓信息
    @Override
    public void queryPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.queryByPage(page,queryVo);
    }


    //根据公寓id删除公寓详情信息
    @Override
    public void removeApartmentById(Long id){
        //考虑是否删除公寓对应房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if(count>0){
            //如果count大于0说明当前的公寓下面有对应的房间
            //我们需要抛出异常，不会正常返回结果(业务正确)，使用全局异常处理器进行捕捉，返回对应的错误码给前端
            //DELETE_ERROR(207, "请先删除子集")
            //自定义异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //1.删除公寓
        removeById(id);
        //2.根据公寓id删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //3.根据公寓id删除标签
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelLambdaQueryWrapper);
        //4.根据公寓id删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);
        //5.根据公寓id删除配置信息
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper= new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);

    }

    //根据id查询公寓详细信息
    @Override
    public ApartmentDetailVo queryApartmentById(Long id) {
        //1. id查询公寓信息详情
        ApartmentInfo apartmentInfo =getById(id);
        if (apartmentInfo == null) {return null;}
        //2. 公寓id查询配套详情 (推荐直接调用配套的mapper,少了一层service的方法)
        //正常: 调用对方的service (事务的传播行为)
        //不正常: 1. service 只有一行 和 对应一个数据库动作 我们的service直接mapper依然能mapper加到当前事务
        //       2. 省一层代码结果  service xx -> mapper xx
        //        3. 有意避免循环注入的问题
        List<FacilityInfo> facilityInfoList =  facilityInfoMapper.queryByApartmentId(id);
        //3. 公寓id查询图片集合
        List<GraphVo> graphVoList = graphInfoMapper.queryByItemId(ItemType.APARTMENT,id);
        //4. 根据公寓id查询标签集合
        List<LabelInfo> labelInfoList = labelInfoMapper.queryByApartmentId(id);
        //5. 公寓id查询杂费和杂费名集合
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryByApartmentId(id);

        //6. 合并到ApartmentDetailVo对象返回即可
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        //将原对象的属性赋值给目标对象的相同属性! 属性值迁移
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }
}



